package com.ys.controller.web.hr;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.hr.ThrUserArchives;
import com.ys.entity.hr.ThrUserArchivesRecord;
import com.ys.entity.hr.ThrUserIdCardPhoto;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工档案表
 */
@Controller
@RequestMapping("/web/hr/thrUserArchives")
public class WebThrUserArchivesController {
	private static final Logger logger = LoggerFactory.getLogger(WebThrUserArchivesController.class);

	@Autowired
	private CommonApiService<ThrUserArchives> commonApiService;


	public Map<String, Object> getEntity(Long id){
		Map<String,Object> entity = new ThrUserArchives()
				.addField("A.HeadUserId + '/' + U.UserName AS headUserName")
				.addField("O.DeptName AS deptName")
				.addField("R.Name AS roleName")
				.addField("BA1.HigherLevelPath + ',' + BA1.Name AS nativePlaceName")
				.addField("BA2.HigherLevelPath + ',' + BA2.Name AS workCityName")
				.addField("O1.OrgName AS belongCompanyName")
				.addField("B.VReMark AS officeName")
				.addField("B.VName AS officeTxt")
				.addLeftJoin("TS_OrganizationInfo O ON O.Orgid = A.DeptId AND 1=1")
				.addLeftJoin("TS_UserInfo U ON U.UserId = A.HeadUserId AND 1=1")
				.addLeftJoin("YS_Role R ON R.Id = A.RoleId AND 1=1")
				.addLeftJoin("TB_BaseInfoArea BA1 ON BA1.Code = A.NativePlace AND 1=1")
				.addLeftJoin("TB_BaseInfoArea BA2 ON BA2.Code = A.WorkCity AND 1=1")
				.addLeftJoin("TS_OrganizationInfo O1 ON O1.Orgid = A.BelongCompany AND 1=1")
				.addLeftJoin("TB_BaseInfo B ON B.Id = A.OfficeId AND 1=1")
				//.addLeftJoin("THR_OperateCompanyApply CA ON CA.VGsBm = A.BelongCompany AND 1=1")
				.setId(id)
				.queryFirstMap();
		return entity;

	}


	public Map<String, Object> setExtraParam(Map<String,Object> entity){
		Map<String, Object> extraParam = new HashMap<String, Object>();
		//关联数据
		//extraParam.put("roleIdAttributeName", entity.get("roleIdAttribute").toString().equals("0")?"职能":"运营");//岗位属性(0职能、1运营)
		if(entity.get("headUserName")!=null)extraParam.put("headUserName", entity.get("headUserName"));
		if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
		if(entity.get("roleName")!=null)extraParam.put("roleName", entity.get("roleName"));
		if(entity.get("nativePlaceName")!=null)extraParam.put("nativePlaceName", entity.get("nativePlaceName"));
		if(entity.get("workCityName")!=null)extraParam.put("workCityName", entity.get("workCityName"));
		if(entity.get("belongCompanyName")!=null)extraParam.put("belongCompanyName", entity.get("belongCompanyName"));
		if(entity.get("officeName")!=null)extraParam.put("officeName", entity.get("officeName"));
		if(entity.get("officeId")!=null)extraParam.put("officeId", entity.get("officeId"));
		if(entity.get("officeTxt")!=null)extraParam.put("officeTxt", entity.get("officeTxt"));
		//获取用户照片数据
		List<ThrUserIdCardPhoto> thrUserIdCardPhoto = new ThrUserIdCardPhoto().setUserId(entity.get("userId").toString()).queryList();
		if(thrUserIdCardPhoto!=null ){
			if(thrUserIdCardPhoto.size()>=1){
				if(thrUserIdCardPhoto.get(0).getUserPhoto()!=null)extraParam.put("userPhoto1", thrUserIdCardPhoto.get(0).getUserPhoto());
				if(thrUserIdCardPhoto.get(0).getIdCardfront()!=null)extraParam.put("idCardfront1", thrUserIdCardPhoto.get(0).getIdCardfront());
				if(thrUserIdCardPhoto.get(0).getIdCardBack()!=null)extraParam.put("idCardBack1", thrUserIdCardPhoto.get(0).getIdCardBack());
			}
			if(thrUserIdCardPhoto.size()>=2){
				if(thrUserIdCardPhoto.get(1).getUserPhoto()!=null)extraParam.put("userPhoto2", thrUserIdCardPhoto.get(1).getUserPhoto());
				if(thrUserIdCardPhoto.get(1).getIdCardfront()!=null)extraParam.put("idCardfront2", thrUserIdCardPhoto.get(1).getIdCardfront());
				if(thrUserIdCardPhoto.get(1).getIdCardBack()!=null)extraParam.put("idCardBack2", thrUserIdCardPhoto.get(1).getIdCardBack());
			}
			if(thrUserIdCardPhoto.size()>=3){
				if(thrUserIdCardPhoto.get(2).getUserPhoto()!=null)extraParam.put("userPhoto3", thrUserIdCardPhoto.get(2).getUserPhoto());
				if(thrUserIdCardPhoto.get(2).getIdCardfront()!=null)extraParam.put("idCardfront3", thrUserIdCardPhoto.get(2).getIdCardfront());
				if(thrUserIdCardPhoto.get(2).getIdCardBack()!=null)extraParam.put("idCardBack3", thrUserIdCardPhoto.get(2).getIdCardBack());
			}
		}
		return extraParam;
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		try {
			Map<String,Object> entity = getEntity(id);
			Map<String, Object> extraParam = setExtraParam(entity);

			List<Map<String, Object>> itemList = new ThrUserArchivesRecord()
					.setUserId(entity.get("userId").toString())
					.queryMap();

			model.addAttribute("onProcess", 0);
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.THR_UserArchives,itemList);
			return commonApiService.toEdit(model,id, new ThrUserArchives(), MenuId.THR_UserArchives, extraParam, subGridConfig);
		}catch (Exception e) {
			logger.error("员工档案表编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}


	/*
	 * 详情
	 */
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		try {
			Map<String,Object> entity = getEntity(id);
			Map<String, Object> extraParam = setExtraParam(entity);

			List<Map<String, Object>> itemList = new ThrUserArchivesRecord()
					.setUserId(entity.get("userId").toString())
					.queryMap();
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.THR_UserArchives,itemList);
			subGridConfig.setButtonArr(new ArrayList<>());
			return commonApiService.toDetail(model,id, new ThrUserArchives(), MenuId.THR_UserArchives, extraParam, subGridConfig);
		}catch (Exception e) {
			logger.error("员工档案表编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}



}






